//
//  UserViewController.m
//  豆瓣
//
//  Created by dllo on 15/11/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "UserViewController.h"
#import "ActionShareViewController.h"
#import "FilmShareViewController.h"
#import "LoginViewController.h"
#import <sqlite3.h>

@interface UserViewController () <UITableViewDataSource, UITableViewDelegate, loginViewDelegate>

@property (nonatomic, retain) UITableView *tableV;

@end

@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我的";
    
    
  /********************************** 用户界面 ***************************************/
 
    
//    [self changeItem];
    NSUserDefaults *userLogin = [NSUserDefaults standardUserDefaults];
    if ([[userLogin objectForKey:@"state"] isEqualToString:@"F"]) {
         NSLog(@"%@, %@", [userLogin objectForKey:@"user"], [userLogin objectForKey:@"state"]);
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登录" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
    }
    else {
           NSLog(@"%@, %@", [userLogin objectForKey:@"user"], [userLogin objectForKey:@"state"]);
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:@"%@ 已登录", [userLogin objectForKey:@"user"]] style:UIBarButtonItemStylePlain target:self action:nil];
    }
    
    self.tableV = [[UITableView alloc] initWithFrame:self.view.frame];
    self.tableV.scrollEnabled = NO;
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    [self.tableV release];
}

// 右上角显示 - 登录/已登录
- (void)changeItem {
    
    NSUserDefaults *userLogin = [NSUserDefaults standardUserDefaults];
    

    if ([[userLogin objectForKey:@"state"] isEqualToString:@"F"]) {
        NSLog(@"2");
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登录" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
    }
    else {
        NSLog(@"1");
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:@"%@ 已登录", [userLogin objectForKey:@"user"]] style:UIBarButtonItemStylePlain target:self action:nil];
    }
    
    [self.tableV reloadData];
}

// 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 3;
}

// 行间距
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 62;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSArray *arr = [NSArray arrayWithObjects:@"我的活动", @"我的电影", @"清除缓存", nil];
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:[arr objectAtIndex:indexPath.row]];
    
    cell.textLabel.text = [arr objectAtIndex:indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:20];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 验证是否登录
    // 打开数据库
    static sqlite3 *DB = nil;
    NSString *dbPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *filePath = [dbPath stringByAppendingPathComponent:@"dataBase.db"];
    sqlite3_open(filePath.UTF8String, &DB);
    
    // 查询有没有状态为登录("T")
    NSString *sqlStr = [NSString stringWithFormat:@"SELECT * FROM user WHERE  userState LIKE '%%%@%%'", @"T"];
    sqlite3_stmt *stmt = nil;
    sqlite3_prepare(DB, sqlStr.UTF8String, -1, &stmt, NULL);
    
    NSString *name = nil;
    
    while (SQLITE_ROW == sqlite3_step(stmt)) {
        
        const unsigned char *userName = sqlite3_column_text(stmt, 1);
        name = [NSString stringWithUTF8String:(const char *)userName];
    }
    
    // 如果没有登录, 提示登录
    if (name == nil) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"还没有登录,是否登录? " preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            [self.navigationController pushViewController:loginVC animated:YES];
            [loginVC release];
        }];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            return;
        }];
        
        [alert addAction:yesAction];
        [alert addAction:cancelAction];
        
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
    }
    
    // 如果已登录, 按请求跳转
    else {
        
        // 跳转到活动收藏界面
        if (0 == indexPath.row) {
            
            ActionShareViewController *actionShareVC = [[ActionShareViewController alloc] init];
            actionShareVC.name = name;
            [self.navigationController pushViewController:actionShareVC animated:YES];
            [actionShareVC release];
            
        }
        
        // 跳转到电影收藏界面
        if (1 == indexPath.row) {
            
            FilmShareViewController *filmShareVC = [[FilmShareViewController alloc] init];
            filmShareVC.name = name;
            [self.navigationController pushViewController:filmShareVC animated:YES];
            [filmShareVC release];
            
        }
        
        // 清除缓存(注销)
        if (2 == indexPath.row) {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"是否注销?" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                NSUserDefaults *userLogin = [NSUserDefaults standardUserDefaults];
                [userLogin setObject:@"F" forKey:@"state"];
                [userLogin setObject:name forKey:@"user"];
                
                // 将状态改为F
                NSString *sqlStr = [NSString stringWithFormat:@"UPDATE user SET userState = 'F' WHERE userName = '%@'", name];
                sqlite3_exec(DB, sqlStr.UTF8String, NULL, NULL, NULL);
                
                // 关闭数据库
                sqlite3_close(DB);
                [self changeItem];
            }];
            
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                return;
                
            }];
            
            [alert addAction:yesAction];
            [alert addAction:cancelAction];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
        }
    }
}

// 跳转到登录界面
- (void)rightAction {
    
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    loginVC.delegate = self;
    [self.navigationController pushViewController:loginVC animated:YES];
    
    [loginVC release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
